package com.zwh.maijiu.web;


import com.zwh.maijiu.model.EbUser;
import com.zwh.maijiu.service.EbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("login")
public class LoginController {
    @Autowired
    EbUserService ebUserService;
    //1去登录
    @RequestMapping("/to_login")
    public String toLigin(HttpSession session){
        return "login";
    }
    //1.1登录
    @RequestMapping("/login")
    public String login(HttpSession session, EbUser ebUser){
        System.out.println("===========进入login方法========ebUser"+ebUser);
        //1.调用service层查询用户是否存在
        EbUser lgnUser = ebUserService.login(ebUser);
        //2.判断用户是否存在
        if(lgnUser==null){
            //携带错误信息带到首页
            session.setAttribute("msg","用户名或密码错误,请重新登录");
            return "redirect:/login/to_login";
        }
        //登录成功
        session.removeAttribute("msg");
        //3.将用户信息传到首页  显示欢迎 xxx
        session.setAttribute("lgnUserName",lgnUser.getEuUserName());
        //登录成功跳转到首页  重定向
        return "redirect:/front/index";
    }

    //2注册
    @RequestMapping("/to_register")
    public String toRegister(){
        return "register";
    }
    //2.2
    @RequestMapping("/register")
    public String register(HttpSession session, EbUser ebUser){
        //1.调用service进行注册
        ebUser.setEuStatus(1);//设置用户状态，默认普通用户
        EbUser lgnUser=ebUserService.regisrer(ebUser);
        //2.自动登录
        if(lgnUser!=null){
            session.setAttribute("lgnUserName",ebUser.getEuUserName());
            return "register";

        }else{
            session.setAttribute("msg","用户名或密码错误,请重新注册");
            return "to_register";
        }
    }

    //3.退出
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("lgnUserName");
        return "redirect:/front/index";
    }
}
